package com.wipz.eoa.iphone.util;

import java.io.BufferedReader;
import java.io.Reader;
import java.sql.Clob;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataUtil {

	/**
	 * ��CLOBת��String ,��̬����
	 * 
	 * @param clob
	 *            �ֶ�
	 * @return �����ִ��������ִ��󣬷���null
	 */
	public final static String clob2String(Clob clob) {
		if (clob == null) {
			return null;
		}
		StringBuffer sb = new StringBuffer(65535);// 64K
		Reader clobStream = null;// ����һ�����������
		try {
			clobStream = clob.getCharacterStream();
			char[] charArr = new char[60000];// ÿ�λ�ȡ60K
			int index = 0;
			while ((index = clobStream.read(charArr)) != -1) {
				sb.append(charArr, 0, index);
			}
		} catch (Exception ex) {
			sb = null;
		} finally {
			try {
				if (clobStream != null) {
					clobStream.close();
				}
			} catch (Exception e) {
			}
		}
		if (sb == null) {
			return "";
		} else {
			return sb.toString();
		}
	}

	/**
	 * 
	 * @param obj
	 *            2012-1-13 ����07:35:20
	 */
	public static Map filterClob(Object obj) {
		Map m = (Map) obj;
		Object k;
		Object v;
		Clob c;
		BufferedReader in;
		String line;
		for (Iterator i = m.keySet().iterator(); i.hasNext();) {
			k = i.next();
			v = m.get(k);
			if (v instanceof Clob) {
				c = (Clob) v;
				m.put(k, clob2String(c));
			}
		}
		return m;
	}

	/**
	 * ����html�����ַ�,��̬����(<��>��&��' '��\n)
	 * 
	 * @param htmlContent
	 *            :����
	 * @return
	 */
	public final static String htmlEncoder(String htmlContent) {
		if (htmlContent == null || "".equals(htmlContent)) {
			return "";
		}
		htmlContent = htmlContent.replaceAll("&", "&amp;");
		htmlContent = htmlContent.replaceAll("<", "&lt;");
		htmlContent = htmlContent.replaceAll(" ", "&nbsp;");
		htmlContent = htmlContent.replaceAll(">", "&gt;");
		htmlContent = htmlContent.replaceAll("\n", "<br>");
		htmlContent = htmlContent.replaceAll("\"", "\'");
		return htmlContent;
	}

	/**
	 * ����ͼƬ��Ϣ
	 */
	public final static String filterImg(String str) {
		str =  str.replaceAll("<[Ii][Mm][Gg].*/>", "");
		str = str.replaceAll("<p>", "");
		str = str.replaceAll("</p>", "");
		str = str.replaceAll("&nbsp;", "");
		return str;
	}

	/**
	 * ��ȡͼƬ·��
	 * 
	 * @param str
	 *            ���ݴ�
	 * @return ͼƬ·�� 2012-1-31 ����07:11:44
	 */
	public final static String getImgStr(String str) {
		Matcher m = Pattern
				.compile(
						"<[Ii][Mm][Gg]\\s+[Aa][Ll][Tt]=\"\\w*\"\\s+[Ss][Rr][Cc]=\"([\\w:/\\.\\?=\\&]+)\"\\s+/>")
				.matcher(str);
		StringBuffer bf = new StringBuffer();
		int i = 0;
		while (m.find()) {
			if (i++ != 0) {
				bf.append(";");
			}
			bf.append(m.group(1));
		}
		return bf.toString();
	}

}
